/**
 * 
 */
package com.yangtao.prototype.listener;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.yangtao.framework.service.ServiceLocator;
import com.yangtao.prototype.system.dao.MenuTreeDAO;
import com.yangtao.prototype.system.entity.MenuTree;

/**
 * @author 杨涛
 * 
 */
public class ResourceLoaderListenner implements ServletContextListener {
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub

	}

	@SuppressWarnings("unchecked")
	@Override
	public void contextInitialized(ServletContextEvent event) {
		InputStream stream = null;
		MenuTree first = null;
		try {
			MenuTreeDAO mtd = (MenuTreeDAO) ServiceLocator.getInstance()
					.getService(MenuTreeDAO.class);
			Integer maxlevel = mtd
					.findUnique("select max(level) from MenuTree");
			maxlevel = maxlevel == null?0:maxlevel;
			for (int i = maxlevel; i > 0; i--) {
				mtd.batchExecute("delete from MenuTree where level=?", i);
			}
			SAXReader reader = new SAXReader();
			stream = getClass().getClassLoader()
					.getResourceAsStream("menu.xml");
			Document document = reader.read(stream);
			Element root = document.getRootElement();
			Iterator modules = root.elementIterator("Module");
			int count = 0;
			while (modules.hasNext()) {
				Element element = (Element) modules.next();
				first = new MenuTree();
				first.setId(element.attributeValue("id"));
				first.setName(element.attributeValue("name"));
				first.setLevel(Integer.parseInt(element
						.attributeValue("level")));
				first.setIcon(element.attributeValue("icon"));
				first.setUrl(element.attributeValue("url"));
				first.setDispOrder(count);
				addMoudles(element, first, count);
				mtd.save(first);
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		} finally {
			if (stream != null) {
				try {
					stream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 递归加载子级菜单
	 * @param element
	 * @param mt
	 * @param dispOrder
	 */
	@SuppressWarnings("unchecked")
	private void addMoudles(Element element, MenuTree mt,int dispOrder){
		List<MenuTree> subMenus = new ArrayList<MenuTree>();
		for(Iterator it =element.elementIterator();it.hasNext();){
			Element item = (Element) it.next();
			MenuTree subMenu = new MenuTree();
			subMenu.setId(item.attributeValue("id"));
			subMenu.setName(item.attributeValue("name"));
			subMenu.setLevel(Integer.parseInt(item
					.attributeValue("level")));
			subMenu.setIcon(item.attributeValue("icon"));
			subMenu.setUrl(item.attributeValue("url"));
			subMenu.setParentMenu(mt);
			subMenu.setDispOrder(dispOrder);
			dispOrder++;
			addMoudles(item, subMenu, dispOrder);
			subMenus.add(subMenu);
		}
		mt.setSubMenus(subMenus);
	}
}
